import { TextAsset, director } from "cc";
import { BaseClass } from "../../../Framework/Scripts/Managers/BaseClass";
import { AbNames, EventMsgKes } from "../Config/ResConfig";
import { GameData } from "./GameData";
import { ResManager } from "../../../Framework/Scripts/Managers/ResManager";
import { ExcelMgr } from "../../../Framework/Scripts/Managers/ExcelMgr";
import { DWLVTYPE, DWTYPE, RewardType } from "../Config/GameCfg";
import { Itopic } from "../../AssetPackage/Props/Game/DT_TZ";
import { GetNewPlayerRes } from "../../AssetPackage/Props/Hall/GetUserReward";
import { GetHD_LoginRewardGetRes, GetHD_LoginRewardRes } from "../../AssetPackage/Props/Hall/GetHDLoginReward";
import { GameAnswerEndBroadcast, IThemeMatchGameEndItem, IThemeMatchGameRecord, IThemeMatchItem, IUserInfo, ThemeMatchGameAnswerBroadcast, ThemeMatchGameAnswerReq, ThemeMatchGameEndBroadcast, ThemeMatchGameNextBroadcast, ThemeMatchGameNextGroupRes, ThemeMatchGameRecordRes, ThemeMatchGameRes, ThemeMatchPPItem, ThemeMatchPPRes, ThemeMatchPPStopRes, ThemeMatchRes, ThemeMatchResInfo, ThemeMatchStarRes } from "../../AssetPackage/Props/Game/DT_ZT";
import { FriendJoinBroadcast, FriendStartPKRes, FriendJoinNotify, FriendJoinOperateRes } from "../../AssetPackage/Props/Game/DT_PK";

/**
 * 测试本地数据
 */
export class TestData extends BaseClass {
    private static _inst: TestData;
    topics: Itopic[];
    topicCur:number;
    tidCur: number; //当前选择的题目id
    userIds: string[]; 

    tids: number[]; //当前的题目组
    idx: number;

    public static get inst(): TestData {
        if (this._inst == null) {
            this._inst = new TestData();
        }

        return this._inst;
    }

    constructor() {
        super()  
        //当前题目索引
        console.log('=========注意测试时候先加载GamePlay 的bundle===========')
  

        GameData.inst.userInfo.userId = '1001';
        this.userIds = [ GameData.inst.userInfo.userId, '1002'];

        // [1001,1002,1003,1004,1005,1006,1007,1008,1009,1010];
        this.tids = [1001,1002];
        this.idx = 0;

        this.topicCur = 0;
    }
       
    public onLoginRes(){
        // let res:LoginBackRes ={
        //     code: 0,
        //     userId: '1001',
        //     userInfo: {
        //         userId: GameData.inst.userInfo.userId,
        //         name: GameData.inst.userInfo.name,
        //         avator: GameData.inst.userInfo.avator,
        //         ballNum: GameData.inst.userInfo.ballNum,
        //         ticket: GameData.inst.userInfo.ticket,
        //         dwWTInfo: {
        //             dwType: DWLVTYPE.DWLVTYPE1,
        //             dwLv: 10,
        //             dwXzNum: 11,
        //             dwRank: 12
        //         },
        //         dwZqInfo: {
        //             dwType: DWLVTYPE.DWLVTYPE1,
        //             dwLv: 13,
        //             dwXzNum: 14,
        //             dwRank: 15
        //         },
        //         dwMzInfo: {
        //             dwType: DWLVTYPE.DWLVTYPE1,
        //             dwLv: 16,
        //             dwXzNum: 17,
        //             dwRank: 18
        //         },
        //         userGame: {
        //             challengeNum: 50,
        //             challengeWin: 33,
        //             challengeXX: 25,
        //             challengeDS: 88,
        //             ranks: [2, 3, 4], //文体 足球 名族
        //         },
        //         ranks: 0,
        //         score: 0
        //     },
        //     hallNet: undefined
        // }
        // console.log('==========onLoginRes===========',res);
        // director.emit(EventMsgKes.msgLoginRes,res);
    }

    public onGameHallRes(){
        // let data:GameHallRes ={
        //     isNewPlayer: 1,
        //     rewards: [],
        //     userInfo: undefined
        // }

        // let r1:IReward = {
        //     rewardType: RewardType.TYpe_QiuNum,
        //     rewardNum: 100
        // } 
        // let r2:IReward = {
        //     rewardType: RewardType.Type_Quan,
        //     rewardNum: 88
        // }
        // data.rewards.push(r1,r2);
         
        // console.log('=======onGameHallRes=======',data)
        // director.emit(EventMsgKes.msgGameHallRes,data);
    }

    //获取用户数据
    public onGetUserInfoRes(){
        // let data:GetUserInfoRes = {
        //     userInfo: {
        //         userId: GameData.inst.userInfo.userId,
        //         name: GameData.inst.userInfo.name,
        //         avator: GameData.inst.userInfo.avator,
        //         ballNum: GameData.inst.userInfo.ballNum,
        //         ticket: GameData.inst.userInfo.ticket,
        //         dwWTInfo: {
        //             dwType: DWLVTYPE.DWLVTYPE1,
        //             dwLv: 10,
        //             dwXzNum: 11,
        //             dwRank: 12
        //         },
        //         dwZqInfo: {
        //             dwType:DWLVTYPE.DWLVTYPE1,
        //             dwLv: 13,
        //             dwXzNum: 14,
        //             dwRank: 15
        //         },
        //         dwMzInfo: {
        //             dwType:DWLVTYPE.DWLVTYPE1,
        //             dwLv: 16,
        //             dwXzNum: 17,
        //             dwRank: 18
        //         },
        //         userGame: {
        //             challengeNum: 50,
        //             challengeWin: 33,
        //             challengeXX: 25,
        //             challengeDS: 88,
        //             ranks:[2,3,4], //文体 足球 名族
        //         },
        //         ranks: 0,
        //         score: 0
        //     }
        // }
        // director.emit(EventMsgKes.msgGetUserInfoRes,data);
    }

    public onGetNewPlayerRes(){
        let data: GetNewPlayerRes = {
            code: 0
        }
        director.emit(EventMsgKes.msgGetNewPlayerRes,data);

        //广播用户数据变化
        GameData.inst.userInfo.ballNum += 100;
        GameData.inst.userInfo.ticket += 88;
        director.emit(EventMsgKes.msgUpdateUserInfoNotice,GameData.inst.userInfo);
    }

    public onGetHD_LoginRewardRes(){
        // let data: GetHD_LoginRewardRes = {
        //     rewards: [],
        //     isGetTaday: 1
        // }

        // let rewaordIds = [0,1,2,3,4,5,6];
        // let rewaordNums = [100,200,300,400,500,600,700];
        // let isGets =   [0,0,1,1,1,1,1];
        // let isTadays = [1,1,0,1,1,1,1];
        // for (let i = 0; i < 7; i++) {
        //     const cfg: IRewardHD = {
        //         rewardId: rewaordIds[i],
        //         isGet: isGets[i],
        //         reward: {
        //             rewardType: RewardType.TYpe_QiuNum,
        //             rewardNum: rewaordNums[i],
        //         },
        //         isToday: isTadays[i]
        //     }
        //     data.rewards.push(cfg);
        // }
        // director.emit(EventMsgKes.msgonGetHD_LoginRewardRes,data);
    }

    public onGetHD_LoginRewardGetRes(){
        let data:GetHD_LoginRewardGetRes = {
            code: 0,
        }
    }

    //测试数据
    public onDailyChallenge(type: number){
        // let data: DailyChallengeRes = {
        //     code: 0,
        //     dcType: type,
        //     topicTotal: 15,
        //     listReward: [],
        // }

        // let types = [RewardType.Type_Quan,RewardType.TYpe_QiuNum];
        // for (let i = 0; i < 3; i++) {
        //     let cfg:IStageReward = {
        //         topicNum: 5 + (i * 5),
        //         reward: {
        //             rewardType:types[Math.floor(Math.random()*types.length)],
        //             rewardNum: i * 5 + 10,
        //         }
        //     }
        //     data.listReward.push(cfg)
        // }

        // console.log('=======onDailyChallenge=======',data)
        // director.emit(EventMsgKes.msgDailyChallengeRes,data);
    }

    //挑战赛开始游戏返回
    public onGamePlayTzResTest(){
        this.tids = [1001,1002,1003,1004,1005,1006,1007,1008,1009,1010]
        this.idx = 0;

        this.tidCur = this.tids.shift();

        // let data:GamePlayTzRes ={
        //     code: 0,
        //     topicTotal: 0,
        //     topic:{
        //         tidx: this.idx,
        //         time: 10,
        //         topicId: this.tidCur
        //     }
        // }
        // director.emit(EventMsgKes.msgGamePlayTzRes,data);
    }

    public onThemeMatchRes(){
        let data:ThemeMatchRes = {
            list:[]
        }

        let types = [DWTYPE.DW_WENTI,DWTYPE.DW_ZUQIU,DWTYPE.DW_MINGZU];
        let lvTypes = [DWLVTYPE.DWLVTYPE1,DWLVTYPE.DWLVTYPE2,DWLVTYPE.DWLVTYPE3];
        let lvs = [10,20,30];
        let dwXzs = [50,100,150];
        let avators = [
            GameData.inst.defaultLogo,   
            GameData.inst.defaultLogo,      
            GameData.inst.defaultLogo,    
            ]
        for (let i = 0; i < 3; i++) {
            let item:IThemeMatchItem = {
                type: types[i],
                people: 10000+ (i+5)*100,
                dwType: lvTypes[i],
                dwLv: lvs[i],
                dwXzNum: dwXzs[i],
                list:[]
            }
            for (let j = 0; j < 3; j++) {
                let temp: ThemeMatchResInfo = {
                    name: "name_" + i,
                    avator: avators[i],
                    dwLv: lvs[j],
                    dwXzNum: dwXzs[j]
                }
                item.list.push(temp);
            }
            data.list.push(item);
        }

        director.emit(EventMsgKes.msgThemeMatchRes,data);
    }

    public onThemeMatchPPRes(type: number){
        let data:ThemeMatchPPRes = {
            code: 0,
            userList:[],
        }

        let ranks = [12,14];
        let dwTypes = [DWLVTYPE.DWLVTYPE1,DWLVTYPE.DWLVTYPE2];
        let lvs = [15,16];
        let winCounts = [115,216];
        for (let i = 0; i < 2; i++) {
            const cfg: ThemeMatchPPItem = {
                userId: (1000 + i).toString(),
                name: "测试" + i,
                avator: GameData.inst.defaultLogo,
                rank: ranks[i],
                dwType: type ,
                dwLv: lvs[i],
                winCount: winCounts[i],
                dwLvType: dwTypes[i],
            }
            data.userList.push(cfg);
        }

        director.emit(EventMsgKes.msgThemeMatchPPRes,data);
    }

    public onThemeMatchPPStopReq(){
        let data:ThemeMatchPPStopRes = {
            code: 0
        }

        director.emit(EventMsgKes.msgThemeMatchPPStopRes,data);
    }

    public onThemeMatchStarReq(){
        let data: ThemeMatchStarRes = {
            code: 0
        }
        director.emit(EventMsgKes.msgThemeMatchStarRes,data);
    }

    public onThemeMatchGameReq(){
        this.tids = [1001,1002,1003,1004,1005,1006,1007,1008,1009,1010]
        this.idx = 0;

        this.tidCur = this.tids.shift();
        let data: ThemeMatchGameRes = {
            users: [],
            topic: {
                time: 10,
                topicId: this.tidCur,
                tidx: this.idx
            }
        }

        for (let i = 0; i < 2; i++) {
            let cfg: IUserInfo = {
                userId: this.userIds[i],
                name: "测试_" + i,
                avator: GameData.inst.defaultLogo,
                ballNum: '0',
                ticket: '0',
                ranks: '0',
                score: '0'
            };
            if(i== 0){
                cfg.userId = GameData.inst.userInfo.userId;
            }
            data.users.push(cfg);
        }
        director.emit(EventMsgKes.msgThemeMatchGameRes,data);
    }

    //主题赛用户答题
    public onThemeMatchGameAnswerResTest(d: ThemeMatchGameAnswerReq){
        //答案id
        let aid = -1;
        let txt = ResManager.inst.getAsset(AbNames.GamePlay,'Excle/DT',TextAsset) as TextAsset;
        ExcelMgr.inst.AddTable('DT',txt.text);

        let res = ExcelMgr.inst.QueryOne('DT',d.topicId);
        aid = res['Answer'];

        let data:ThemeMatchGameAnswerBroadcast = {
            code: 0,
            userId: d.userId,
            topicId: this.tidCur,
            choiceId: d.choiceId,
            correctChoiceId: aid,
        }
        // console.log('=======onThemeMatchGameAnswerResTest=========',data);
        director.emit(EventMsgKes.msgThemeMatchGameAnswerBroadcast,data);
    }

    //他人答题
    public onThemeMatchGameAnswerResTest2(isDui:boolean){
        //答案id
        let aid = -1;
        let choiceId = -1;
        let txt = ResManager.inst.getAsset(AbNames.GamePlay,'Excle/DT',TextAsset) as TextAsset;
        ExcelMgr.inst.AddTable('DT',txt.text);

        let res = ExcelMgr.inst.QueryOne('DT',this.tidCur);
        aid = res['Answer'];
        console.log('=======res==========',res)
        if(isDui){
            choiceId = aid;
        }else{
            for (let i = 0; i < res['TopicChoiceCount']; i++) {
                if(i !== aid){
                    choiceId = i;
                    break;
                }
            }
        }
    
        let data:ThemeMatchGameAnswerBroadcast = {
            code: 0,
            userId: this.userIds[1],
            topicId: this.tidCur,
            choiceId: choiceId,
            correctChoiceId: aid,
        }
        console.log('=======onThemeMatchGameAnswerResTest2=========',data);
        director.emit(EventMsgKes.msgThemeMatchGameAnswerBroadcast,data);
    }

    //当前题目所有人答完了广播结果
    public GameAnswerEndBroadcast(){
        
        let txt = ResManager.inst.getAsset(AbNames.GamePlay,'Excle/DT',TextAsset) as TextAsset;
        ExcelMgr.inst.AddTable('DT',txt.text);

        let res = ExcelMgr.inst.QueryOne('DT',this.tidCur);
        let aid = res['Answer'];
        let data:GameAnswerEndBroadcast = {
            correctChoiceId: aid
        }
        director.emit(EventMsgKes.msgGameAnswerEndBroadcast,data);
    }

    //下一题
    public ThemeMatchGameNextBroadcastTest(){
        if(this.tids.length > 0){
            this.idx++;
            this.tidCur = this.tids.shift();
            let data:ThemeMatchGameNextBroadcast = {
                topic: {
                    time: 11,
                    topicId: this.tidCur,
                    tidx: this.idx
                }
            }
            console.log('============ThemeMatchGameNextBroadcastTest==============',data);
            director.emit(EventMsgKes.msgThemeMatchGameNextBroadcast,data);
        }else{
            console.log('============ThemeMatchGameNextBroadcastTest1==结算============');
            //结算
            this.ThemeMatchGameEndBroadcast();
        }
    }

    //主题赛结算广播
    public ThemeMatchGameEndBroadcast() {
        let data: ThemeMatchGameEndBroadcast = {
            isWin: 0, //0 赢了 1 输了
            users: [],
        }

        let scores = [120, 340];
        let times = [220, 124];
        for (let i = 0; i < 2; i++) {
            let cfg: IThemeMatchGameEndItem = {
                name: "测试_" + i,
                avator: GameData.inst.userInfo.avator,
                score: scores[i],
                time: times[i],
                userId: this.userIds[i],
            };
            data.users.push(cfg);
        }
        director.emit(EventMsgKes.msgThemeMatchGameEndBroadcast, data);
    }

    //请求下一组返回
    public onThemeMatchGameNextGroupReqTest(){
        let data:ThemeMatchGameNextGroupRes ={
            code: 0
        }
        director.emit(EventMsgKes.msgThemeMatchGameNextGroupRes, data);
    }

    //答题回顾
    public onThemeMatchGameRecordReqTest(){
        let data:ThemeMatchGameRecordRes = {
            rewards:[]
        }

        let txt = ResManager.inst.getAsset(AbNames.GamePlay,'Excle/DT',TextAsset) as TextAsset;
        ExcelMgr.inst.AddTable('DT',txt.text);
        let tids = [1001,1002,1003,1004,1005,1006,1007,1008,1009,1010];
        for (let i = 0; i < tids.length; i++) {
            const tid = tids[i];
            let cfg = ExcelMgr.inst.QueryOne('DT',tid);

            let item:IThemeMatchGameRecord = {
                topicId: tid,
                correctChoiceId: cfg['Answer'],
                choiceId: Math.random() > 0.5 ? cfg['Answer'] : 0
            }
            data.rewards.push(item);
        }
        director.emit(EventMsgKes.msgThemeMatchGameRecordRes, data);
    }

    //好有加入广播
    public onFriendJoinBroadcast(){
        let data:FriendJoinBroadcast = {
            user: {
                userId: "0",
                name: "加入玩家",
                avator:  GameData.inst.defaultLogo,
                ballNum: '10',
                ticket: '10',
                ranks: '10',
                score: '10'
            }
        }
        director.emit(EventMsgKes.msgFriendJoinBroadcast, data);
    }

    public onFriendStartPKReqTest(){
        let data:FriendStartPKRes = {
            code: 0,
            groupId: 1001
        }
        director.emit(EventMsgKes.msgFriendStartPKRes, data);
    }

    public onFriendJoinNotify(){
        let data:FriendJoinNotify = {
            userId: this.userIds[0],
            user: {
                userId: this.userIds[1],
                name: '测试',
                avator: GameData.inst.userInfo.avator,
                ballNum: '0',
                ticket: '0',
                ranks: '0',
                score: '0'
            }
        }
        director.emit(EventMsgKes.msgFriendJoinNotify, data);
    }

    //用户操作
    public onFriendJoinOperateReqTest(opt: number){
        let data: FriendJoinOperateRes = {
            code: 0
        }
        director.emit(EventMsgKes.msgFriendJoinOperateRes, data);
    }

    //每日挑战配置返回
    public onDailyChallengeCfgReqTest(){
        // let data: DailyChallengeCfgRes = {
        //     code: 0,
        //     count: 0
        // }
        // director.emit(EventMsgKes.msgDailyChallengeCfgRes, data);
    }
}